package cn.edu.cug.cs.gtl.plugins.catalog.tree;

import cn.edu.cug.cs.gtl.apis.ui.AbstractApplication;
import cn.edu.cug.cs.gtl.apis.ui.actions.AbstractAction;
import cn.edu.cug.cs.gtl.apis.ui.datake.DatakeApp;
import cn.edu.cug.cs.gtl.apis.ui.datake.DatakeFrame;
import cn.edu.cug.cs.gtl.apis.ui.datake.catalogtree.CatalogTree;
import cn.edu.cug.cs.gtl.apis.ui.tree.TreeNodeData;
import cn.edu.cug.cs.gtl.protos.DataCatalog;
import cn.edu.cug.cs.gtl.utils.postgresql.DataCatalogManager4Postgresql;
//import cn.edu.cug.cs.gtl.utils.postgresql.DataCatalogManager4Postgresql;

import javax.swing.*;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeNode;
import javax.swing.tree.TreePath;
import java.awt.event.ActionEvent;
import java.util.Locale;
import java.util.ResourceBundle;

//新建数据目录这个功能的Action
public class AddChildCatalogAction extends AbstractAction {
    @Override
    public void initialize(AbstractApplication theApp) {
        this.theApp = theApp;
        ResourceBundle resourceBundle = ResourceBundle.getBundle("catalog-resource", Locale.getDefault());

        name = resourceBundle.getString("popupmenu.catalog.tree.add_child_catalog");
        menuItem.setText(name);
        menuItem.addActionListener(this);
        ((CatalogTree) (getDatakeFrame().getCatalogPanel().getTreeComponent())).getCatalogNodePopupMenu().add(menuItem);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        CatalogTree tree = (CatalogTree) getDatakeFrame().getCatalogPanel().getTreeComponent();
        DefaultMutableTreeNode selectNode = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
        if (selectNode == null) return ;

        StringBuilder ChildNodeName = new StringBuilder();
        StringBuilder ChildNodeAlias = new StringBuilder();
        StringBuilder ChildNodeDescription = new StringBuilder();
        boolean ret = InputDialog.showInputCatalogDialog(ChildNodeName, ChildNodeAlias, ChildNodeDescription);
        if (!ret)
            return ;

        DataCatalog childCatalog = DataCatalog.newBuilder().setName(ChildNodeName.toString()).setAlias(ChildNodeAlias.toString())
                .setDescription(ChildNodeDescription.toString()).build();
        //在数据库中插入一条记录
        DataCatalog parentCatalog = (DataCatalog) ((TreeNodeData) (selectNode.getUserObject())).getRefObject();
        ret = new DataCatalogManager4Postgresql().append(parentCatalog.getName(), childCatalog);
        if (!ret) {
            JOptionPane.showMessageDialog(null, "插入失败，可能该名称已存在", "错误", JOptionPane.ERROR_MESSAGE);
            return ;
        }

        DefaultMutableTreeNode newNode = new DefaultMutableTreeNode(new TreeNodeData(childCatalog, childCatalog.getAlias(), true), true);
        newNode.setAllowsChildren(true);

        //将新节点newNode作为选中节点selectNode的最后子节点添加到树中，并通知树的视图。
        DefaultTreeModel treeModel = (DefaultTreeModel) tree.getModel();
        treeModel.insertNodeInto(newNode, selectNode, selectNode.getChildCount());

        //将新节点变为可视
        TreeNode[] nodes = treeModel.getPathToRoot(newNode);
        tree.scrollPathToVisible(new TreePath(nodes));    //构造一个从根节点到新添加节点的树路径，由于我们的树放在一个滚动面板中，这样会告诉外围的滚动面板将路径末端的节点滚动到视图中在·
//        tree.expandPath(tree.getSelectionPath());
    }

    public DatakeFrame getDatakeFrame() {
        return (DatakeFrame) (((DatakeApp)theApp).getMainFrame());
    }
}
